<?
//==========================================
function convTemp($value)
//==========================================
{
    $res = 61.41 - 0.078*$value;
    return($res);
}
//==========================================
function writeCurrentValue($user,$power,$temp,$celcius,$sigmax,$base,$puls)
//==========================================
{
  $file = "bess_$user.cur";
  $date = date("Y-m-d H:i:s");

  if(!$file)
    {
      echo("writeCurrentValue: no file ($file)");
      return;
    }
  
  $out = fopen($file,"w");
  if($out)
    {
      //fprintf($out,"User:%3d Temperature:(%4d) %3.1f  <br>Power:%4d <br>Signal:%3d Base:%3d Puls:%3d ",$user,$temp,$celcius,$power,$sigmax,$base,$puls);
      fprintf($out,"%s User:%3d <h1>%4d Watt</h1> <br>Signal:%3d Base:%3d Puls:%3d ",$date,$user,$power,$sigmax,$base,$puls);
    }
  fclose($out); 
}

//==========================================
function logData($user,$data,$puls)
//==========================================
{ 
  if($user)
    {
      $ymd  = date("Y-m-d");
      $out = fopen("bess-$user-$ymd.log","a");
      if($out)
      {
        $date = date("Y-m-d H:i:s");
        $temp = $date." ".$data."\n";
        fwrite($out,$temp);
      }
      fclose($out);

      updateDaySum($user,$puls);
      updateMonthSum($user,$puls);
      updateYearSum($user,$puls);    
    }  
}


//==========================================
function updateDaySum($user,$puls)
//==========================================
{
  $value = 0;
  $date  = date("Y-m-d");	
  $file  = "bess-$user-$date.sum";

  $in = fopen($file,"r");
  if($in)
    {
      while (!feof($in))
      {
         $row = fgets($in);
         sscanf($row,"%d",$value);
      }
    }  
  fclose($in);

  $value = $value + $puls;

  $out = fopen($file,"w");
  if($out)
    {
      fprintf($out,"%d",$value);
    }
  fclose($out); 
 
}

//==========================================
function updateMonthSum($user,$puls)
//==========================================
{
  $value = 0;
  $date  = date("Y-m");	
  $file  = "bess-$user-$date.sum";

  $in = fopen($file,"r");
  if($in)
    {
      while (!feof($in))
      {
         $row = fgets($in);
         sscanf($row,"%d",$value);
      }
    }  
  fclose($in);

  $value = $value + $puls;

  $out = fopen($file,"w");
  if($out)
    {
      fprintf($out,"%d",$value);
    }
  fclose($out); 
 
}

//==========================================
function updateYearSum($user,$puls)
//==========================================
{
  $value = 0;
  $date  = date("Y");	
  $file  = "bess-$user-$date.sum";

  $in = fopen($file,"r");
  if($in)
    {
      while (!feof($in))
      {
         $row = fgets($in);
         sscanf($row,"%d",$value);
      }
    }  
  fclose($in);

  $value = $value + $puls;

  $out = fopen($file,"w");
  if($out)
    {
      fprintf($out,"%d",$value);
    }
  fclose($out); 
 
}
//==========================================
function readUserConfig($user)
//==========================================
{
  global $bess;
  
  $file = "bess_$user.conf";
  
  if(!$file)
    {
      echo("readConfFile: no file ($file)");
      return;
    }

  $in = fopen($file,"r");
  if($in)
    {
      while (!feof($in))
    {
      $row = fgets($in);
      if(strstr($row,"ITL:"))
        {
          sscanf($row,"%s%d",$junk,$value);
          $bess['intriglev'] = $value;
        }
      if(strstr($row,"UTL:"))
        {
          sscanf($row,"%s%d",$junk,$value);
          $bess['untriglev'] = $value;
        }
    }
    }  
  $nData = $ii;
  fclose($in);
}
//====================================================

$user   = $_GET['user'];
$time   = $_GET['time'];
$puls   = $_GET['puls'];
$power  = $_GET['power'];
$base   = $_GET['base'];
$sigmax = $_GET['sigmax'];
$temp   = $_GET['temp'];

$conf  = $_GET['conf'];

if($conf)
  {
    readUserConfig($conf);
    $trigOn  = $bess['intriglev'];
    $trigOff = $bess['untriglev'];
    echo("user= $conf itl= $trigOn utl= $trigOff");
  }

if($user && $puls)
  {
    $data = "";
    $data = $data." ".$time;
    $data = $data." ".$puls;
    $data = $data." ".$power;
    $data = $data." ".$base;
    $data = $data." ".$sigmax;
    $data = $data." ".$temp;
    logData($user,$data,$puls);
    // Convert to Celcius
    $celcius = convTemp($temp);
    writeCurrentValue($user,$power,$temp,$celcius,$sigmax,$base,$puls);
  }

?>

